from typing import Optional, List, Dict, Any, Union
from datetime import datetime
from pydantic import BaseModel, Field


# 基础模式
class BaseSchema(BaseModel):
    class Config:
        from_attributes = True


# 分类模式
class MCPCategoryBase(BaseSchema):
    name: str
    description: Optional[str] = None
    icon: Optional[str] = None


class MCPCategoryCreate(MCPCategoryBase):
    pass


class MCPCategoryUpdate(BaseSchema):
    name: Optional[str] = None
    description: Optional[str] = None
    icon: Optional[str] = None


class MCPCategory(MCPCategoryBase):
    id: int
    created_at: datetime
    updated_at: datetime


# 工具模式
class MCPToolBase(BaseSchema):
    name: str
    english_name: str
    description: Optional[str] = None
    icon: Optional[str] = None
    category_id: int
    api_endpoint: Optional[str] = None
    parameters_schema: Optional[Dict[str, Any]] = None
    mcp_config: Optional[Dict[str, Any]] = None
    is_active: bool = True


class MCPToolCreate(MCPToolBase):
    pass


class MCPToolUpdate(BaseSchema):
    name: Optional[str] = None
    english_name: Optional[str] = None
    description: Optional[str] = None
    icon: Optional[str] = None
    category_id: Optional[int] = None
    api_endpoint: Optional[str] = None
    parameters_schema: Optional[Dict[str, Any]] = None
    mcp_config: Optional[Dict[str, Any]] = None
    is_active: Optional[bool] = None


class MCPTool(MCPToolBase):
    id: int
    created_at: datetime
    updated_at: datetime
    category: MCPCategory


# 工具详细信息，包含分类信息
class MCPToolDetail(MCPTool):
    pass


# 工具列表，分组显示
class MCPToolsByCategory(BaseSchema):
    category: MCPCategory
    tools: List[MCPTool]


# 订阅模式
class MCPSubscriptionBase(BaseSchema):
    user_id: int
    tool_id: int
    is_active: bool = True
    expiry_date: Optional[datetime] = None


class MCPSubscriptionCreate(MCPSubscriptionBase):
    pass


class MCPSubscriptionUpdate(BaseSchema):
    is_active: Optional[bool] = None
    expiry_date: Optional[datetime] = None


class MCPSubscription(MCPSubscriptionBase):
    id: int
    created_at: datetime
    updated_at: datetime
    tool: MCPTool


# 工具运行参数和结果
class ToolRunRequest(BaseSchema):
    parameters: Dict[str, Any]


class ToolRunResponse(BaseSchema):
    result: Any
    status: str = "success"
    error: Optional[str] = None


# 工具参数响应
class ToolParamsResponse(BaseSchema):
    parameters_schema: Dict[str, Any]
    error: Optional[str] = None 